<?php
declare (strict_types=1);

namespace app\admin\controller\iot;

use app\admin\service\iot\DeviceService;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\response\Json;

/**
 * 设备管理
 */
class Device extends BaseController
{

    /**
     * 获取所有设备类型
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceTypeAll(DeviceService $deviceService): Json
    {
        return $deviceService->deviceTypeAll();
    }

    /**
     * 终端仓库列表
     * @param DeviceService $deviceService
     * @return Json
     */
    public function warehouseList(DeviceService $deviceService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['imei'] = Request::param('imei/s');
        $params['mac_id'] = Request::param('mac_id/s');
        $params['apk'] = Request::param('apk/s');
        $params['install'] = Request::param('install/d');
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $deviceService->warehouseList($params);
    }

    /**
     * 设备列表
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceList(DeviceService $deviceService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['imei'] = Request::param('imei/s', '');
        $params['mac_id'] = Request::param('mac_id/s', '');
        $params['type'] = Request::param('type/d', 0);
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $deviceService->deviceList($params);
    }

    /**
     * 设备列表导出
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceListExport(DeviceService $deviceService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['imei'] = Request::param('imei/s', '');
        $params['mac_id'] = Request::param('mac_id/s', '');
        $params['type'] = Request::param('type/d', 0);
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $deviceService->deviceListExport($params);
    }

    /**
     * 设备通信日志列表
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceLogList(DeviceService $deviceService): Json
    {
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['imei'] = Request::param('imei/s', '');
        return $deviceService->deviceLogList($params);
    }

    /*********************************** 设备操作命令 *************************************/

    /**
     * 出货事件
     * @param DeviceService $deviceService
     * @return Json
     */
    public function shippingOrder(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->shippingOrder($imeis);
    }

    /**
     * 电机出货长度事件
     * @param DeviceService $deviceService
     * @return Json
     */
    public function motorDeliveryLength(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        $motor = Request::param('motor/d', 1);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->motorDeliveryLength($imeis, $motor);
    }

    /**
     * 袋子余量传感器
     * @param DeviceService $deviceService
     * @return Json
     */
    public function allowanceQuery(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->allowanceQuery($imeis);
    }

    /**
 * 定时开关机事件
 * @param DeviceService $deviceService
 * @return Json
 */
    public function timingSwitch(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        $time_type = Request::param('time_type/d', 1);
        $on_time = Request::param('on_time/s', '');
        $off_time = Request::param('off_time/s', '');
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->timingSwitch($imeis, $time_type, $on_time, $off_time);
    }

    /**
     * 音量调整事件
     * @param DeviceService $deviceService
     * @return Json
     */
    public function volumeSet(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        $volume = Request::param('volume/d', 0);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->volumeSet($imeis, $volume);
    }

    /**
     * 设备重启事件
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceRestart(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->deviceRestart($imeis);
    }

    /**
     * 设备日志开关
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceLogCat(DeviceService $deviceService): Json
    {
        $imeis = Request::param('imeis/a', []);
        $log_cat = Request::param('log_cat/d', 0);
        if (!$imeis) {
            return jsonReturn(0, [], '参数错误');
        }
        return $deviceService->deviceLogCat($imeis, $log_cat);
    }

    /**
     * 设备重启事件(手动使用)
     * @param DeviceService $deviceService
     * @return Json
     */
    public function deviceRestarts(DeviceService $deviceService): Json
    {
        //开始2023-1-10
        //新增真实的设备6610002换成6610551（虚拟的）
        $update_mac = [6610463,
            6610462,
            6610249,
            6610247,
            6610239,
            6610238,
            6610210,
            6610187,
            6610186,
            6610185,
            6610184,
            6610183,
            6610181,
            6610177,
            6610176,
            6610175,
            6610174,
            6610173,
            6610172,
            6610171,
            6610170,
            6610169,
            6610168,
            6610167,
            6610165,
            6610164,
            6610163,
            6610162,
            6610161,
            6610160,
            6610159,
            6610158,
            6610157,
            6610156,
            6610155,
            6610154,
            6610153,
            6610151,
            6610150,
            6610149,
            6610146,
            6610144,
            6610142,
            6610141,
            6610132,
            6610129,
            6610128,
            6610127,
            6610126,
            6610125,
            6610124,
            6610123,
            6610122,
            6610121,
            6610120,
            6610119,
            6610118,
            6610117,
            6610116,
            6610113,
            6610112,
            6610111,
            6610110,
            6610109,
            6610108,
            6610107,
            6610106,
            6610105,
            6610103,
            6610065,
            6610064,
            6610063,
            6610062,
            6610061,
            6610060,
            6610059,
            6610058,
            6610056,
            6610055,
            6610054,
            6610053,
            6610052,
            6610051,
            6610050,
            6610049,
            6610048,
            6610047,
            6610046,
            6610045,
            6610042,
            6610041,
            6610040,
            6610039,
            6610038,
            6610037,
            6610036,
            6610035,
            6610034,
            6610033,
            6610032,
            6610030,
            6610028,
            6610027,
            6610026,
            6610025,
            6610024,
            6610023,
            6610022,
            6610021,
            6610020,
            6610019,
            6610018,
            6610016,
            6610015,
            6610014,
            6610013,
            6610012,
            6610011,
            6610010,
            6610008,
            6610007,
            6610006,
            6610005,
            6610004,
            6610003,
            6610001,
            80939,
            82271,
            81138,
            81666,
            81133,
            82289,
            82287,
            82286,
            82285,
            82270,
            82268,
            82267,
            82266,
            82265,
            82264,
            82263,
            82262,
            82261,
            82260,
            82259,
            82258,
            82257,
            82254,
            82253,
            82248,
            82247,
            82246,
            82245,
            82244,
            82243,
            82242,
            82241,
            82237,
            82235,
            82234,
            82233,
            82228,
            82219,
            82218,
            82217,
            82216,
            82207,
            82206,
            82205,
            82204,
            82203,
            82202,
            82201,
            82200,
            82199,
            82198,
            82195,
            82182,
            82181,
            82180,
            82179,
            81369,
            81367,
            81366,
            81345,
            81120,
            81116,
            81115,
            81114,
            81110,
            81092,
            81086,
            81085,
            80955,
            80954,
            80951,
            80950,
            82238,
            6610212,
            6610208,
            6610207,
            6610206,
            6610204,
            6610203,
            6610202,
            6610201,
            6610199,
            6610198,
            6610197,
            6610196,
            6610195,
            6610194,
            6610193,
            6610192,
            6610191,
            6610190,
            6610189,
            6610182,
            6610180,
            6610179,
            6610178,
            6610075
        ];
        //需要改的虚拟设备
        $update_mac_x = [
            6610616,
            6610615,
            6610614,
            6610574,
            6610573,
            6610572,
            6610561,
            6610560,
            6610559,
            6610558,
            6610557,
            6610556,
            6610555,
            6610554,
            6610553,
            6610552,
            6610550,
            6610549,
            6610548,
            6610547,
            6610546,
            6610545,
            6610544,
            6610543,
            6610542,
            6610541,
            6610540,
            6610539,
            6610538,
            6610537,
            6610536,
            6610535,
            6610534,
            6610533,
            6610532,
            6610531,
            6610530,
            6610529,
            6610528,
            6610527,
            6610526,
            6610525,
            6610524,
            6610523,
            6610522,
            6610521,
            6610520,
            6610519,
            6610518,
            6610517,
            6610516,
            6610515,
            6610514,
            6610513,
            6610512,
            6610511,
            6610510,
            6610509,
            6610508,
            6610507,
            6610506,
            6610505,
            6610504,
            6610503,
            6610502,
            6610501,
            6610500,
            6610499,
            6610498,
            6610497,
            6610496,
            6610495,
            6610494,
            6610493,
            6610440,
            6610439,
            6610437,
            6610435,
            6410144,
            6410143,
            6410142,
            6410135,
            6410133,
            6410132,
            6410129,
            6410125,
            6410115,
            6410114,
            6410072,
            6410071,
            6410070,
            6410069,
            6410068,
            6410067,
            6410066,
            6410065,
            6410064,
            6410063,
            6410042,
            6410041,
            6410040,
            6410039,
            6410038,
            6410037,
            6410036,
            6410035,
            6410034,
            6410033,
            6410032,
            6410031,
            6410030,
            6410029,
            6410028,
            6410027,
            6410026,
            6410025,
            6410024,
            6410023,
            6410022,
            6410021,
            6410020,
            6410019,
            6410018,
            6410017,
            6410016,
            6410015,
            6410014,
            6610551,
            6410081,
            6410080,
            6410079,
            6410078,
            6410077,
            6410076,
            6410075,
            6410074,
            6410073,
            6410062,
            6410061,
            6410060,
            6410059,
            6410058,
            6410057,
            6410056,
            6410055,
            6410054,
            6410053,
            6410171,
            6410165,
            6410164,
            6410163,
            6410162,
            6410161,
            6410103,
            6410102,
            6410101,
            6410100,
            6410099,
            6410098,
            6410096,
            6410095,
            6410094,
            6410093,
            6410092,
            6410091,
            6410090,
            6410089,
            6410088,
            6410087,
            6410086,
            6410085,
            6410084,
            6610427,
            6610464,
            6610466,
            6610467,
            6610476,
            6610478,
            6610480,
            6610483,
            6610485,
            6610486,
            6610488,
            6610489,
            6610490,
            6610563,
            6610564,
            6610565,
            6610567,
            6610569,
            6610604,
            6610605,
            6610607,
            6610608,
            6610609,
            6610611,
            6610613,
            6610426,
            6410082,
            6610428,
            6610465,
            6610468,
            6610469,
            6610470,
            6610471,
            6610472,
            6610473,
            6610474,
            6610475,
            6610477,
            6610479,
            6610481,
            6610482,
            6610484,
            6610487,
            6610491,
            6610566,
            6610568,
            6610570,
            6610571,
            6610606,
            6610610,
            6610612
        ];
        $arr = array_merge($update_mac,$update_mac_x);
        $mac = Db::connect('iot')
            ->table('machine_stock')
            ->whereIn('serial',$arr)
            ->select()->toArray();
        $mac_ids = array_column($mac,'secret');
        return $deviceService->deviceRestart($mac_ids);
    }


}
